//
//  YPHSDepositoryView.m
//  ChengHuiTong
//
//  Created by CHT-Technology on 2017/6/8.
//  Copyright © 2017年 张文权. All rights reserved.
//

#import "YPHSDepositoryView.h"

@interface YPHSDepositoryView (){
    
    NSString *_hs_account; //电子账户ID
    NSString *_hs_cardBank; //银行卡字母代码，用户显示银行logo
    NSString *_hs_card_realname; //账户名
    NSString *_hs_card_number; //卡号
    YPHSDepositoryViewState _currentState; //当前的状态
}

/**
 状态
 */
@property (nonatomic,assign)YPHSDepositoryViewState state;
/**
 绑定事件回调
 */
@property (nonatomic,copy)dispatch_block_t bindBlock;
/**
 开通事件回调
 */
@property (nonatomic,copy)dispatch_block_t openBlock;

@end

@implementation YPHSDepositoryView

- (instancetype)initWithFrame:(CGRect)frame
                        state:(YPHSDepositoryViewState)state{
    
    self = [super initWithFrame:frame];
    if (self) {
        
        self.state = state;
        self.backgroundColor = [UIColor whiteColor];
        ViewRadius(self, 5.f);
        
    }
    return self;
}

- (void)layoutSubviews{
    
    [super layoutSubviews];

    [self addSubviewsWithState:self.state];
    
}

/**
 添加自视图

 @param state 状态
 */
- (void)addSubviewsWithState:(YPHSDepositoryViewState)state{
    
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    CGFloat imageTop = autoScaleH(11);
    CGFloat btnTop = autoScaleH(15);
    CGFloat left = autoScaleH(15);
    
    if (state == YPHSDepositoryViewHasBind) { //已绑卡

        //银行logo
        UIImageView *logo = [[UIImageView alloc]initWithFrame:CGRectMake(left, imageTop,115, autoScaleH(31))];
        logo.contentMode = UIViewContentModeScaleAspectFit;
        logo.image = ImageNamed(_hs_cardBank);
        [self addSubview:logo];
        //测试logo
//        [self testBankLogoWithImageView:logo];
        
        
        //名字
        UILabel *nameLabel = [UILabel creatWithFrame:CGRectMake(left, autoScaleH(55), 100, autoScaleH(15))
                                                font:[UIFont systemFontOfSize:autoScaleW(14)]
                                                text:_hs_card_realname
                                           textColor:UIColorFromString(@"#FA4B4E")
                                       textAlignment:0];
        [self addSubview:nameLabel];
        
        //卡号
        UILabel *accountLabel = [UILabel creatWithFrame:CGRectMake(left, autoScaleH(77), autoScaleW(200), autoScaleH(17))
                                                font:[UIFont fontWithName:@"DIN-Bold" size:autoScaleW(17)]
                                                text:_hs_card_number
                                           textColor:nameLabel.textColor
                                       textAlignment:0];
        [self addSubview:accountLabel];
        
        //修改
        UIButton *motifyBtn = [UIButton creatWithFrame:CGRectMake(autoScaleW(265), btnTop, autoScaleW(65), autoScaleH(26))
                                                  font:[UIFont systemFontOfSize:autoScaleW(12)]
                                                  text:@"修改"
                                             textColor:UIColorFromString(@"#FF7733")
                                                action:@selector(motifyBtnClick)
                                                target:self];
        ViewBorderRadius(motifyBtn, motifyBtn.height/2.f, 1, colorWithRGB(255, 77, 77));
        [motifyBtn setBackgroundImage:[LGToolMethods createImageWithColor:kColor_AppNavColor] forState:UIControlStateHighlighted];
        [motifyBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
        [self addSubview:motifyBtn];
        
        //切换
        UIButton *changeStateBtn = [self creatChangeBtnWithTop:autoScaleH(63) showBackground:YES];
        [self addSubview:changeStateBtn];
        
    }else if (state == YPHSDepositoryViewNotBind){ //未绑卡
        
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(left, imageTop, 55, 55)];
        imageView.image = ImageNamed(@"cgwkt");
        [self addSubview:imageView];
        
        UILabel *label = [UILabel creatWithFrame:CGRectMake(imageView.right + autoScaleW(10), autoScaleH(33), autoScaleW(120), autoScaleH(15))
                                            font:[UIFont systemFontOfSize:autoScaleW(12)]
                                            text:@"您当前未绑定银行卡"
                                       textColor:UIColorFromString(@"#666666")
                                   textAlignment:0];
        [self addSubview:label];
        
        //切换
        UIButton *changeStateBtn = [self creatChangeBtnWithTop:btnTop showBackground:NO];
        [self addSubview:changeStateBtn];
        
        //绑定银行卡
        UIButton *bindBtn = [UIButton creatWithFrame:CGRectMake(autoScaleW(220), autoScaleH(63), autoScaleH(110), changeStateBtn.height)
                                                  font:[UIFont systemFontOfSize:autoScaleW(12)]
                                                  text:@"立即绑定银行卡"
                                                action:@selector(bindBtnClick)
                                                target:self];
        bindBtn.backgroundColor = UIColorFromString(@"#FF7733");
        ViewRadius(bindBtn, bindBtn.height/2.f);
        [self addSubview:bindBtn];
        
        
    }else{
        
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(left, imageTop, 55, 55)];
        imageView.image = ImageNamed(@"hsyhlg");
        [self addSubview:imageView];
        
        //标题
        UILabel *titleLabel = [UILabel creatWithFrame:CGRectMake(imageView.right + autoScaleW(13),autoScaleH(19) , self.width - imageView.right - 30, autoScaleH(15))
                                            font:[UIFont systemFontOfSize:autoScaleW(14)]
                                            text:@"徽商银行"
                                       textColor:UIColorFromString(@"#FA4B4E")
                                   textAlignment:0];
        [self addSubview:titleLabel];
        
        //内容
        UILabel *contentLabel = [UILabel creatWithFrame:CGRectMake(titleLabel.left, autoScaleH(44) , titleLabel.width, autoScaleH(17))
                                                 font:[UIFont systemFontOfSize:autoScaleW(14)]
                                                 text:@"HUISHANG BANK"
                                            textColor:titleLabel.textColor
                                        textAlignment:0];
        [self addSubview:contentLabel];
        
        //银行存管
        UILabel *cgLabel = [UILabel creatWithFrame:CGRectMake(0, imageView.bottom + (self.height - imageView.bottom - contentLabel.height)/2.f, imageView.width + imageView.left*2,contentLabel.height)
                                                   font:[UIFont systemFontOfSize:autoScaleW(12)]
                                                   text:@"银行存管"
                                              textColor:UIColorFromString(@"#F0181C")
                                          textAlignment:1];
        [self addSubview:cgLabel];
        
        //切换
        UIButton *changeBtn = [[UIButton alloc]initWithFrame:CGRectMake(autoScaleW(265), autoScaleH(63), autoScaleW(65), autoScaleH(26))];
        changeBtn.titleLabel.font = [UIFont systemFontOfSize:autoScaleW(12)];
        changeBtn.backgroundColor = UIColorFromString(@"#FF7733");
        ViewRadius(changeBtn, changeBtn.height/2.f);
        [self addSubview:changeBtn];
        
        if (self.state == YPHSDepositoryViewNotActive) { //已开通未激活
            changeBtn.frame = CGRectMake(autoScaleW(225), autoScaleH(63), autoScaleW(110), autoScaleW(26));
            [changeBtn setTitle:@"立即绑卡激活" forState:0];
            [changeBtn addTarget:self action:@selector(bindBtnClick) forControlEvents:UIControlEventTouchUpInside];
            
        }else if (self.state == YPHSDepositoryViewOpenedNormal){ //已开通且正常
            titleLabel.text = @"徽商银行电子账户";
            contentLabel.font = [UIFont fontWithName:@"DIN-Bold" size:autoScaleW(17)];
            contentLabel.text = _hs_account;
            [changeBtn setTitle:@"切换" forState:0];
            [changeBtn addTarget:self action:@selector(changeStateClick) forControlEvents:UIControlEventTouchUpInside];
            
        }else if (self.state == YPHSDepositoryViewNotOpen){ //未开通
            changeBtn.frame = CGRectMake(autoScaleW(235), autoScaleH(63), autoScaleW(96), autoScaleW(26));
            [changeBtn setTitle:@"立即开通" forState:0];
            [changeBtn addTarget:self action:@selector(openHSBankClick) forControlEvents:UIControlEventTouchUpInside];
            
        }else{ //初始状态
            changeBtn.hidden = YES;
            
        }
    }
    
}

/**
 创建切换按钮
 
 @param top 顶部距离
 @param show 是否显示背景
 @return 按钮
 */
- (UIButton *)creatChangeBtnWithTop:(CGFloat)top
                     showBackground:(BOOL)show{
    
    
    UIButton *changeStateBtn = [UIButton creatWithFrame:CGRectMake(autoScaleW(265), top, autoScaleW(65), autoScaleH(26))
                                                   font:[UIFont systemFontOfSize:autoScaleW(12)]
                                                   text:@"切换"
                                              textColor:show?[UIColor whiteColor]:UIColorFromString(@"#FF7733")
                                                 action:@selector(changeStateClick)
                                                 target:self];
    if (show) {
        changeStateBtn.backgroundColor = UIColorFromString(@"#FF7733");
        ViewRadius(changeStateBtn, changeStateBtn.height/2.f);
    }else{
        ViewBorderRadius(changeStateBtn, changeStateBtn.height/2.f, 1, colorWithRGB(255, 77, 77));
        [changeStateBtn setBackgroundImage:[LGToolMethods createImageWithColor:kColor_AppNavColor] forState:UIControlStateHighlighted];
        [changeStateBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    }
    
    return changeStateBtn;

}

/**
 添加切换动画
 */
- (void)addAnimation{
    
    if (![self.layer animationForKey:@"animation"]) {
        CATransition *animation = [CATransition animation];
        animation.timingFunction = UIViewAnimationCurveEaseInOut;
        animation.fillMode = kCAFillModeForwards;
        animation.type = kCATransitionFade;
        animation.duration = 0.3;
        [self.layer addAnimation:animation forKey:@"animation"];
    }
}

#pragma mark -- Actions
/**
 切换状态(银行卡账户 <-> 电子账户)
 */
- (void)changeStateClick{
    
    if (self.state == YPHSDepositoryViewOpenedNormal) { //电子账户切换至银行卡账户
        //判断是否绑卡
        if (_hs_card_number.length == 0 && _hs_card_realname.length == 0 && _hs_cardBank.length == 0) {
            self.state = YPHSDepositoryViewNotBind;
        }else{
            self.state = YPHSDepositoryViewHasBind;
        }
      
    }else{
        
        self.state = YPHSDepositoryViewOpenedNormal;
    }
    
    [self addAnimation];
    
    [self setNeedsLayout];
    
}

/**
 修改银行卡
 */
- (void)motifyBtnClick{
    
    !self.bindBlock?:self.bindBlock();
}

/**
 绑定银行卡
 */
- (void)bindBtnClick{
    
    !self.bindBlock?:self.bindBlock();
}

/**
 开通银行存管
 */
- (void)openHSBankClick{
    
    !self.openBlock?:self.openBlock();
}

/**
 更新子视图
 
 @param hs_state 徽商电子账户状态0：未开通 1：已开通且正常 2：已开通待激活
 @param hs_account 徽商电子账户ID，19位纯数字，未开通时则为空字符串
 @param hs_cardBank 银行卡字母代码，未绑卡时则为空字符串
 @param hs_card_realname 银行卡账户名，未绑卡时则为空字符串
 @param hs_card_number 银行卡卡号，未绑卡时则为空字符串
 */
- (void)updateSubViewsWithHsState:(NSString *)hs_state
                        hsAccount:(NSString *)hs_account
                       hsCardBank:(NSString *)hs_cardBank
                   hsCardRealname:(NSString *)hs_card_realname
                     hsCardNumber:(NSString *)hs_card_number{
    
    _hs_account = hs_account;
    _hs_cardBank = hs_cardBank;
    _hs_card_number = hs_card_number;
    _hs_card_realname = hs_card_realname;
    //是否已经开通
    if (hs_state.integerValue == 0) {
        self.state = YPHSDepositoryViewNotOpen;
    }else if (hs_state.integerValue == 1){
        self.state = YPHSDepositoryViewOpenedNormal;
    }else{
        self.state = YPHSDepositoryViewNotActive;
    }
    if (_currentState != self.state) [self setNeedsLayout];
    
}

/**
 复原
 */
- (void)restore{
    
    self.state = YPHSDepositoryViewInitial;
    [self setNeedsLayout];
    
}

/**
 处理回调事件
 
 @param bindBlcok 绑定银行卡事件
 @param openBlock 开通存管事件
 */
- (void)configActionsWithBindBlock:(dispatch_block_t)bindBlcok
                         openBlock:(dispatch_block_t)openBlock{
    
    self.bindBlock = bindBlcok;
    self.openBlock = openBlock;
    
}


/**
 银行logo测试

 @param logo 测试的视图
 */
- (void)testBankLogoWithImageView:(UIImageView *)logo{
    
    NSArray *strArr = @[@"icbc",@"abc",@"boc",@"ccb",@"comm",@"cmb",@"ceb",@"hxb",@"cmsb",@"spdb",@"cgb",@"cncb",@"cib",@"psbc",@"pab"];
    NSMutableArray *imageArr = [NSMutableArray array];
    for (NSString *str in strArr) {
        
        [imageArr addObject:ImageNamed(str)];
        
    }
    logo.animationImages = imageArr;
    logo.animationDuration = 15;
    logo.animationRepeatCount = 10;
    [logo startAnimating];
}


@end
